// 浅拷贝实现方式

// 1.Object.assign
const target = { a: 1, b: 2, c: 3 };
const source = { a: 7, b: 8, c: 9, d: 10 };
console.log(Object.assign(target, source));

// 2.ES6拓展运算符
console.log({ ...target, ...source });

// 3.Array.prototype.concat
const arr = [1, 2, { name: "nordon" }];
const newArr = arr.concat();
newArr[2].name = "wy";
console.log(arr);
console.log(newArr);

// 4.Array.prototype.slice
const targetArr = [1, 2, 4, 5, 6, 7];
console.log(targetArr.slice());

// 5.
const clone = (target) => {
  if (target !== null && typeof target === "object") {
    // 对象类型
    const cloneTarget = Array.isArray(target) ? [] : {};
    for (const key in target) {
      if (Object.hasOwnProperty.call(target, key)) {
        cloneTarget[key] = target[key];
      }
    }
    return cloneTarget;
  } else {
    // 基础类型 直接返回
    return target;
  }
};
// 测试
const testArr = [{ name: 123, age: 345, gender: 324 }];
const testObj = {
  name: 123,
  age: 345,
  gender: 324,
  friend: [{ name: 3423, age: 234 }],
};
console.log(clone(testArr));
console.log(clone(testObj));
